﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Select
{   
    /// <summary>
    /// 当设定条件是in InDataList = true
    /// 判断每行的录入结果 是否完全在设定的数组呢
    /// 如设定[30，31][35，36][40，41][45，46]
    /// 
    /// 当设定不在时 InDataList = false
    /// 如设定[1，2，3，4，5，6，7，8，9]
    /// 当结果在1-9范围内 算对，不再算错
    /// </summary>
    [Serializable]
    [VerfityAttribute(Id = "VerfityS0100", Name = "选择项结果为特定项目时抽出", CategoryType = EnumCategoryType.选择, Model = true)]
    public class VerfityS0100 : VerfityOneResult
    {
        private CustomCollection<StringArray> _datalist = new CustomCollection<StringArray>() { Description = "指定数据列表" };

        public bool InDataList { get; set; }


        [Category("指定数据列表"), Description("输入指定数据项目")]
        [TypeConverter(typeof(CustomCollectionConverter<StringArray>))]
        [RefreshProperties(RefreshProperties.All)]
        public CustomCollection<StringArray> DataList
        {
            get { return _datalist; }
            //set { _rangelist = value; }
        }

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (entryCommon.Equals("30"))
            {
                return false;
            }

            if (result == null)
            {
                return false;
            }
             

            bresult = CheckSpecificCharacter(result, GetSpeicficResult, CheckspecificResult);
            
            if (bresult)
            {
                System.Console.WriteLine("VerfityS1000");
            }

            return bresult;
        }

        private   string GetSpeicficResult(string colResult)
        {
            var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, colResult);
            return splitResult.GetSelect();
        }

        protected   bool CheckspecificResult(string result)
        {   
         
            
            bool bresult = false;            
            if (!String.IsNullOrEmpty(result))
            {
                string[] resultArray = result. Split(new char[] { '.' });
                if (InDataList)
                {
/*                    bresult = DataList.Any(item => resultArray.All(r =>
                                                                       {

                                                                           if (r.Contains("?"))
                                                                           {
                                                                               return true;
                                                                           }
                                                                           else
                                                                           {
                                                                               return item.array.Contains(r);
                                                                           }

                                                                       }));*/

                    bresult = DataList.Any(item => {

                        //取得结果 和设定的每组数组的交集
                        IEnumerable<string> corssResult = item.array.Intersect(resultArray);
                        //如果交集内容和设定的一致
                        //说明结果中包含设定的内容
                        //抽出
                        if (corssResult.SequenceEqual(item.array))
                        {
                            return true;
                        }
                        return false;
                    });
                }
                else
                {
                   bresult = DataList.Any(item => resultArray.Any(r => !item.array.Contains(r)));
                }
            }
            return bresult;
        }
    }
}
